//
//  SegmentController.m
//  HappyBaby
//
//  Created by lanou on 15/11/28.
//  Copyright © 2015年 华哥. All rights reserved.
//

#import "SegmentController.h"
#import "CheckMoreController.h"
#import "YJSegmentedControl.h"
#import "Define.h"
@interface SegmentController () <YJSegmentedControlDelegate>

@end

@implementation SegmentController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.translucent = NO;
    
    if (!_category_id) {
        return;
    }
    // 创建子控制器
    [self createChildViewControllers];
    
    // 创建segment
    int segment = _category_id -1;
    [self createUIsegmentedControl:segment];
}

// 创建segment
- (void)createUIsegmentedControl:(int) category_id{
    // 创建需要的选择器
    NSArray *segmentArray = @[@"亲子", @"玩具", @"童书"];
    //
    YJSegmentedControl *segment = [YJSegmentedControl segmentedControlFrame:CGRectMake(0, 0, kScreenWidth, 35*kScaleY) titleDataSource:segmentArray backgroundColor:[UIColor whiteColor] titleColor:[UIColor grayColor] titleFont:[UIFont fontWithName:@".Helvetica Neue Interface" size:kScreenWidth/22] selectColor:[UIColor colorWithRed:99/255.0 green:157/255.0 blue:236/255.0 alpha:1] buttonDownColor:[UIColor colorWithRed:99/255.0 green:157/255.0 blue:236/255.0 alpha:1] Delegate:self selectSegment:category_id];
    [self.view addSubview:segment];
}

// 创建子控制器
- (void)createChildViewControllers
{
    for (int i = 0; i < 3; i++) {
        CheckMoreController *checkMoreVc = [[CheckMoreController alloc] init];
        checkMoreVc.category_id = i+1;// 这是用来请求拼接请求链接获取数据的
        checkMoreVc.view.frame = CGRectMake(0, 0, kScreenWidth, 603 * kScaleY);
        [self.view addSubview:checkMoreVc.view];
        [self addChildViewController:checkMoreVc];
        
    }
}

#pragma mark --实现代理的方法
- (void)segumentSelectionChange:(NSInteger)selection
{
    
    CheckMoreController *firstVc = self.childViewControllers[0];
    CheckMoreController *secondVc = self.childViewControllers[1];
    CheckMoreController *thirdVc = self.childViewControllers[2];
    // 更改frame
    [UIView animateWithDuration:0.2 animations:^{
        firstVc.view.frame = CGRectMake(-kScreenWidth * selection, 0, kScreenWidth, firstVc.view.height);
        secondVc.view.frame = CGRectMake(-kScreenWidth * (selection - 1), 0, kScreenWidth, secondVc.view.height);
        thirdVc.view.frame = CGRectMake(-kScreenWidth * (selection - 2), 0, kScreenWidth, thirdVc.view.height);
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
